$(function(){
    var body = $('body');
    //register
    $('#username').focusout(function(){
        var username = $(this).val();
        if(username != ""){
            $('.accept-username').hide();
            $('.error-username').hide();
            $.ajax({
                type:"POST",
                url:Root_url+"register/checkUsername",
                dataType:"json",
                data:{'username': username},
                success:(function(data){
                    if(data.status == "ok"){
                        $('.accept-username').show();
                        userOk = 1;
                    }else{
                        $('.error-username').show();
                        userOk = 0;
                    }
                })
            });
        }

    });

    $('#password').focusout(function(){
        var password = $(this).val();
        var accept = $('.accept-password');
        var error = $('.error-password');
        accept.hide();
        error.hide();
        if(password.length >= 6){
            accept.show();
        }else{
            error.show();
        }
    });

    $('#email').focusout(function(){
        var email = $(this).val();
        var accept = $('.accept-email');
        var error = $('.error-email');
        accept.hide();
        error.hide();
        if(email != ""){
            if(validateEmail(email)){
                $.ajax({
                    type:"POST",
                    url:Root_url+"register/checkEmail",
                    dataType:"json",
                    data:{'email': email},
                    success:(function(data){
                        if(data.status == "ok"){
                            accept.show();
                        }else{
                            error.text("Địa chỉ email đã được sử dụng").show();
                        }
                    })
                });
            }else{
                error.text("Địa chỉ email không hợp lệ").show();
            }
        }else{
            error.text("Chưa nhập email").show();
        }
    });

    $('#createAccount').click(function(){
        //User
        var username = $('#username').val();
        if(username != ""){
            $.ajax({
                type:"POST",
                url:Root_url+"register/checkUsername",
                dataType:"json",
                data:{'username': username},
                success:(function(data){
                    if(data.status == "ok"){
                        $('.accept-username').show();
                    }else{
                        $('.error-username').show();
                    }
                })
            });
        }
        //Password
        var passOk = 0;
        var password = $('#password').val();
        if(password.length >= 6){
            $('.accept-password').show();
            passOk = 1;
        }else{
            $('.error-password').show();
            passOk = 0;
        }
        //Email
        var email = $('#email').val();
        if(email != ""){
            if(validateEmail(email)){
                $.ajax({
                    type:"POST",
                    url:Root_url+"register/checkEmail",
                    dataType:"json",
                    data:{'email': email},
                    success:(function(data){
                        if(data.status == "ok"){
                            $('.accept-email').show();
                        }else{
                            $('.error-email').text("Địa chỉ email đã được sử dụng").show();
                        }
                    })
                });
            }else{
                $('.error-email').text("Địa chỉ email không hợp lệ").show();
            }
        }else{
            $('.error-email').text("Chưa nhập email").show();
        }

        if($('.error-username').css('display') == 'none' && passOk == 1 && $('.error-email').css('display') == 'none'){
            $('#submit').submit();
        }
    });

    $('#loginAccount').click(function(){
        var username = $('#username').val();
        var password = $('#password').val();

        $.ajax({
            type:"POST",
            url:Root_url+"login/doLogin",
            dataType:"json",
            data:{'username': username, 'password': password},
            success:(function(data){
                if(data.status == "ok"){
                    $('.login-error-notice').hide();
                    $('#submit').submit();
                }else{
                    $('.login-error-notice').show();
                }
            })
        });
    });

    $('.question-answer').find('ul').find('li').click(function(){
        $('.question-box').show('slow');
    });

    shortcut.add("esc", function() {
        //var pluginMedia = $('#pluginMedia');
        var survey = $('.tab-survey-single');
        $('.survey-wrapper').hide("drop", {
            direction: "up"
        });
        //pluginMedia.hide();
        setTimeout(function(){
            survey.hide();
        }, 300);

    });

    body.on('click', '.header-user-info', function(){
       $('.header-user-menu').toggle();
    });
});

function validateEmail(email) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return re.test(email);
}

function closeSurvey(obj){
    obj = $(obj);
    $('.survey-wrapper').hide("drop", {
        direction: "up"
    });
    setTimeout(function(){
        $('.tab-survey-single').hide();
    }, 300);
}